<?php

namespace app\models;

use Yii;
use yii\db\Query;
/**
 * This is the model class for table "gky_qyh_sp_goabroad".
 *
 * @property integer $id
 * @property integer $proposer_id
 * @property integer $approver_id
 * @property string $reason
 * @property string $place
 * @property string $start_time
 * @property string $end_time
 * @property string $agree_time
 * @property string $create_time
 * @property string $work_unit
 * @property string $print_code
 * @property integer $status
 * @property string $signature
 */
class GkyQyhSpGoabroad extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_qyh_sp_goabroad';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['proposer_id', 'approver_id', 'status', 'start_time', 'end_time', 'create_time',  'place', 'reason', 'work_unit','print_code'], 'required'],
            [['id', 'proposer_id', 'approver_id','status'], 'integer'],
            [['start_time', 'end_time', 'agree_time', 'create_time'], 'safe'],
            [['reason', 'place', 'work_unit','print_code'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'proposer_id' => 'Proposer ID',
            'approver_id' => 'Approver ID',
            'reason' => 'Reason',
            'place' => 'Place',
            'start_time' => 'Start Time',
            'end_time' => 'End Time',
            'agree_time' => 'Answer Time',
            'create_time' => 'Fill Time',
            'work_unit' => 'Work Unit',
            'print_code' => 'Print Code',
        ];
    }
    public static function agreeGoabroad($printCode,$opinion){
        $model= GkyQyhSpGoabroad::findOne(['print_code'=>$printCode]);
        $model->status=1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
    public static function refuseGoabroad($printCode,$opinion){
        $model= GkyQyhSpGoabroad::findOne(['print_code'=>$printCode]);
        $model->status=-1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
    public static function getGoabroad($printCode){
        return (new Query())
                        ->select('u.avatar,u.name,l.*,u1.name approver,u1.signature')
                        ->from('gky_qyh_sp_goabroad as l')
                        ->leftJoin('gky_qyh_user as u','l.proposer_id=u.id')
                        ->leftJoin('gky_qyh_user as u1','l.approver_id=u1.id')
                        ->where(['print_code' => $printCode])
                        ->one();
    }
}
